package com.wyp168.juc.volatile1;

/**
 * @ClassName VolatileDemo001
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/4 21:40
 */
class Fruit {
    private volatile int num = 100;

    public void sale () {
        if (num > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出第" + num-- + "个水果，剩余: " + num);
        }
    }
}
public class VolatileDemo001 {
    public static void main(String[] args) {
        Fruit fruit = new Fruit();
        new Thread(() -> {
            for (int i = 0; i < 200; i++) {
                fruit.sale();
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 200; i++) {
                fruit.sale();
            }
        }, "BB").start();
    }
}
